// pages/navigation/new/schedule/schedule.js
const app = getApp()
Page({
  data: {
    TabCur: 0,
    // 日程轴
    agendaList: [],
    color:['olive','cyan','mauve','blue','yellow','pink']
  },
  onLoad: function (options) {
    wx.cloud.database().collection('schedule_info')
      .orderBy('time', 'asc')
      .get()
      .then(res => {
        // console.log('返回的数据',res.data);
        var list = res.data;
        var agendaList = [];
        var date = '';
        for (var i = 0; i < list.length; i++) {
          var _date = this.getDate(list[i].time);
          list[i].time = this.getTime(list[i].time);
          // console.log('日期',_date);
          if (_date == date) {
            agendaList[agendaList.length - 1].todayList.push(list[i])
          } else {
            var item = {
              date: _date,
              todayList: []
            }
            item.todayList.push(list[i]);
            agendaList.push(item);
            date = _date;
          }
          // console.log('agendaList', i, agendaList);
        }
        for(var i = 0; i < agendaList.length; i++){
          agendaList[i].color = this.data.color[i];
        }
        this.setData({
          agendaList: agendaList
        })
        console.log('data里的agendaList',this.data.agendaList);
      }).catch(err => {
        console.error(err);
      })
  },
  getDate: function (date) {
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return m + '月' + d + '日';
  },
  getTime: function (date) {
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    return h+':'+m;
  },
  nav:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './detail/detail?id='+id,
    })
  }
})